#!/usr/bin/python

import httplib, urllib, sys, os, time

dir_name = sys.argv[1]

files = os.listdir(dir_name)
files.sort()

for file_name in files:
    while True:
        file_path = dir_name + "/" + file_name
        print file_path
        img_data = open(file_path).read()
        headers = {'filename' : file_name}
        # h = httplib.HTTPConnection("localhost", 8080)
        try:
            h = httplib.HTTPConnection('booksmap.appspot.com')
            h.request('POST', '/img_upload', img_data, headers)
            r = h.getresponse()
            break
        except:
            print "HTTP Request Error"
            print "retry"
        time.sleep(1)
